/**
 * Copyright (c) 2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

Toolbar.Text = new Class(
{
	Extends: Toolbar.Base,
	
	initialize: function(textarea, options)
	{
		this.textarea = $(textarea);
		this.parent(textarea, options);
		this.textarea.addEvent('keypress', this.onShortcut.bindWithEvent(this));
	},
	
	onShortcut: function(evt)
	{
		if (evt.control && !evt.alt)
		{
			this.buttons.each(function(bt)
			{
				if (bt.length > 2 && bt[2] == evt.key)
				{
					this['cb_' + bt[0]]();
					evt.stop();
//					$break();
				}
			}, this);
		}
	},
	
	
	// text selection
	
	getSelection: function()
	{
		if (document.selection)
		{
			var sel = document.selection.createRange();
			return (sel.parentElement && sel.parentElement().id != this.textarea.id) ? null : sel.text;
		}
		else if (this.textarea.setSelectionRange)
		{
			var nStart = this.textarea.selectionStart;
			var nEnd   = this.textarea.selectionEnd;
			return this.textarea.value.substring(nStart, nEnd);
		}
	},
	
	
	// selected text manipulations
	
	replaceSelection: function(str)
	{
		this.textarea.focus();
		
		if (document.selection)
		{
			var sel = document.selection.createRange();
			if (sel.parentElement().id != this.textarea.id) {
				return;
			}
			sel.text = str;
		}
		else if (this.textarea.setSelectionRange)
		{
			var scrollPos = this.textarea.scrollTop;
			var nStart    = this.textarea.selectionStart;
			var nEnd      = this.textarea.selectionEnd;
			var pos       = nStart + str.length;
			
			this.textarea.value = this.textarea.value.substring(0, nStart) + str + this.textarea.value.substring(nEnd);
			this.textarea.setSelectionRange(pos, pos);
			this.textarea.scrollTop = scrollPos;
		}
	},
	
	encloseSelection: function(open, close)
	{
		var text = open + this.getSelection() + (close || open);
		this.replaceSelection(text);
	},
	
	replaceInSelection: function(regexp, str, before)
	{
		var text = str + this.getSelection().replace(regexp, (before || '') + str);
		this.replaceSelection(text);
	},
	
	selectionStartLinesWith: function(str)
	{
		this.replaceInSelection(/\n/gi, str, '\n');
	},
	
	
	// add text
	
	prependText: function(str)
	{
		this.textarea.value = str + this.textarea.value;
	},
	
	appendText: function(str)
	{
		this.textarea.value += str;
	},
	
	insertText: function(str)
	{
		this.textarea.focus();
		
		if (document.selection)
		{
			var sel = document.selection.createRange();
			if (sel.parentElement().id != this.textarea.id) {
				return;
			}
			sel.text = sel.text + str;
		}
		else if (this.textarea.setSelectionRange)
		{
			var scrollPos = this.textarea.scrollTop;
			var nEnd      = this.textarea.selectionEnd;
			var pos       = nEnd + str.length;
			
			this.textarea.value = this.textarea.value.substring(0, nEnd) + str + this.textarea.value.substring(nEnd);
			this.textarea.setSelectionRange(pos, pos);
			this.textarea.scrollTop = scrollPos;
		}
	}
});